package com.ruoyi.project.shortUrl.log.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.shortUrl.log.domain.ShortUrlRequestLog;
import com.ruoyi.project.shortUrl.log.service.IShortUrlRequestLogService;
import com.ruoyi.project.system.user.domain.User;

/**
 * 短链接访问日志Controller
 * 
 * @author ruoyi
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/short/log")
public class ShortUrlRequestLogController extends BaseController
{
    private String prefix = "shortUrl/log";

    @Autowired
    private IShortUrlRequestLogService shortUrlRequestLogService;

    @RequiresPermissions("short:log:view")
    @GetMapping()
    public String log()
    {
        return prefix + "/log";
    }

    /**
     * 查询短链接访问日志列表
     */
    @RequiresPermissions("short:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShortUrlRequestLog shortUrlRequestLog)
    {
    	User u = ShiroUtils.getSysUser();
    	if(!u.isAdmin()) {
    		shortUrlRequestLog.setCreateUserId(u.getUserId().toString());
    	}
    	
        startPage();
        List<ShortUrlRequestLog> list = shortUrlRequestLogService.selectShortUrlRequestLogList(shortUrlRequestLog);
        return getDataTable(list);
    }

    /**
     * 导出短链接访问日志列表
     */
    @RequiresPermissions("short:log:export")
    @Log(title = "短链接访问日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShortUrlRequestLog shortUrlRequestLog)
    {
        List<ShortUrlRequestLog> list = shortUrlRequestLogService.selectShortUrlRequestLogList(shortUrlRequestLog);
        ExcelUtil<ShortUrlRequestLog> util = new ExcelUtil<ShortUrlRequestLog>(ShortUrlRequestLog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 新增短链接访问日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存短链接访问日志
     */
    @RequiresPermissions("short:log:add")
    @Log(title = "短链接访问日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShortUrlRequestLog shortUrlRequestLog)
    {
        return toAjax(shortUrlRequestLogService.insertShortUrlRequestLog(shortUrlRequestLog));
    }

    /**
     * 修改短链接访问日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShortUrlRequestLog shortUrlRequestLog = shortUrlRequestLogService.selectShortUrlRequestLogById(id);
        mmap.put("shortUrlRequestLog", shortUrlRequestLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存短链接访问日志
     */
    @RequiresPermissions("short:log:edit")
    @Log(title = "短链接访问日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShortUrlRequestLog shortUrlRequestLog)
    {
        return toAjax(shortUrlRequestLogService.updateShortUrlRequestLog(shortUrlRequestLog));
    }

    /**
     * 删除短链接访问日志
     */
    @RequiresPermissions("short:log:remove")
    @Log(title = "短链接访问日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shortUrlRequestLogService.deleteShortUrlRequestLogByIds(ids));
    }
}
